<!-- 宠物服务订单 -->
<template>
    <div >
        <div class='table-page-search-wrapper'>
            <a-form layout='inline'>
               <a-row :gutter='48'>
                    <a-col :md='8' :sm='24'>
                        <a-form-item label="用户姓名">
                            <a-input v-model="userName" allow-clear placeholder="请输入用户姓名" />
                        </a-form-item>
                    </a-col>
                    <a-col :md='8' :sm='24'>
                        <a-form-item label="用户ID">
                            <a-input v-model="userId" allow-clear placeholder="请输入用户ID" />
                        </a-form-item>
                    </a-col>
                    <a-col :md='8' :sm='24'>
                        <a-form-item label="宠物种类">
                            <a-select v-model="speciesId" placeholder="请选择" allowClear @change="getData">
                                <a-select-option v-for="(item,i) in specList" :key="i" :value="item.id">{{item.name}}</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>
                    <a-col :md='8' :sm='24'>
                        <a-form-item label="宠物门店">
                            <a-select v-model="storeId" placeholder="请选择" allowClear @change="getData">
                                <a-select-option v-for="(item,i) in storeList" :key="i" :value="item.id">{{item.name}}-{{item.town}}-{{item.detailedAddress}}</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>
                    <a-col :md='8' :sm='24'>
                        <a-form-item label="服务项目">
                            <a-select v-model="serviceProjectId" placeholder="请选择" allowClear @change="getData">
                                <a-select-option v-for="(item,i) in serviceProjectList" :key="i" :value="item.id">{{item.name}}</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>
                    <a-col :md='8' :sm='24'>
                        <a-form-item label="订单号">
                            <a-input v-model="orderCode" allow-clear placeholder="请输入订单号" />
                        </a-form-item>
                    </a-col>
                    <a-col :md='8' :sm='24'>
                        <a-form-item label="支付订单号">
                            <a-input v-model="paymentOrderCode" allow-clear placeholder="请输入支付订单号" />
                        </a-form-item>
                    </a-col>
                    <a-col :md='12' :sm='24'>
                        <a-form-item label="订单时间">
                            <div class="flex alignCenter">
                                <a-date-picker v-model="startTime" placeholder="开始时间" valueFormat="YYYY-MM-DD" format="YYYY-MM-DD" @change="getData"/>
                                <div> - </div>
                                <a-date-picker v-model="endTime" placeholder="结束时间" valueFormat="YYYY-MM-DD" format="YYYY-MM-DD" @change="getData"/>
                            </div>
                        </a-form-item>
                    </a-col>
                    <a-col :md='8' :sm='24'>
                        <a-form-item label="支付方式">
                            <a-select v-model="paymentType" placeholder="请选择" allowClear @change="getData">
                                <a-select-option v-for="(item,i) in paymentTypeList" :key="i" :value="item.id">{{item.name}}</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>
                    <a-col :md='8' :sm='24'>
                        <a-form-item label="订单状态">
                            <a-select v-model="orderStatus" placeholder="请选择" allowClear @change="getData">
                                <a-select-option v-for="(item,i) in orderStatusList" :key="i" :value="item.id">{{item.name}}</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>
                    <a-col :md='8' :sm='24'>
                    </a-col>
                    <a-col :md='8' :sm='24'>
                        <a-form-item >
                            <a-button type='primary' style="margin-right: 20px" @click="getData">查询</a-button>
                        </a-form-item>
                    </a-col>
                </a-row>
            </a-form>
        </div>  

        <a-table rowKey="id" :loading="isload" :columns="columns" :data-source="dataList" @change="handleTableChange" :pagination="{ pageSize: 10, total: total ,showSizeChanger: true,pageSize: size, }" size="small" :scroll="{ x: 1800, }">
            <div slot="serviceProjectId" slot-scope="serviceProjectId">
                <span v-for="(item,i) in serviceProjectList" :key="item.id">
                    <a-tag v-if="item.id == serviceProjectId" :color="colors[i%13]"><ellipsis :length="18" tooltip>{{ item.name }}</ellipsis></a-tag>
                </span>
            </div>
            <div slot="speciesId" slot-scope="speciesId">
                <span v-for="(item,i) in specList" :key="item.id">
                    <a-tag v-if="item.id == speciesId" :color="colors[i%13]">{{item.name}}</a-tag>
                </span>
            </div>
            <div slot="storeId" slot-scope="storeId">
                <span v-for="(item,i) in storeList" :key="item.id">
                    <a-tag v-if="item.id == storeId" :color="colors[i%13]">{{item.name}}</a-tag>
                </span>
            </div>
            <div slot="paymentType" slot-scope="paymentType,record">
                <div v-if="paymentType != '' && paymentType && record.orderStatus == '3'">
                    <span v-for="(item,i) in paymentTypeList" :key="item.id">
                        <a-tag v-if="item.id == paymentType" :color="colors[i%13]">{{item.name}}</a-tag>
                    </span>
                </div>
                <div v-else>-</div>
                <!-- <ellipsis :length="18" tooltip>{{ paymentMethod == '' || !paymentMethod ? '-' : paymentMethod }}</ellipsis> -->
            </div>
            <div slot="orderCode" slot-scope="orderCode">
                <ellipsis :length="18" tooltip>{{ orderCode == '' || !orderCode ? '-' : orderCode }}</ellipsis>
            </div>
            <div slot="paymentOrderCode" slot-scope="paymentOrderCode">
                <ellipsis :length="18" tooltip>{{ paymentOrderCode == '' || !paymentOrderCode ? '-' : paymentOrderCode }}</ellipsis>
            </div>
            <div slot="remarks" slot-scope="remarks">
                <ellipsis :length="18" tooltip>{{ remarks == '' || !remarks ? '-' : remarks }}</ellipsis>
            </div>
            <div slot="orderStatus" slot-scope="orderStatus">
                <span v-for="(item,i) in orderStatusList" :key="item.id">
                    <a-tag v-if="item.id == orderStatus" :color="colors[i%13]">{{item.name}}</a-tag>
                </span>
            </div>
            <div slot="operation" slot-scope="text,record">
                <!-- <a-popconfirm
                    title="是否修改当前订单状态为已完成?"
                    v-if="record.orderStatus == '1'"
                    ok-text="Yes"
                    cancel-text="No"
                    @confirm="updateOrderStatus(record,3)"
                >
                    <a-button  type="link">设置完成</a-button>
                </a-popconfirm> -->
                <a-popconfirm
                    title="是否取消当前订单?"
                    v-if="record.orderStatus == '1'"
                    ok-text="Yes"
                    cancel-text="No"
                    @confirm="updateOrderStatus(record,2)"
                >
                    <a-button class="error" type="link">取消订单</a-button>
                </a-popconfirm>
            </div>
        </a-table>
    </div>
</template>

<script>
import { queryPetServiceOrderForm , queryPetStore , queryPetSpecies , queryPetServiceProject , updatePetServiceOrderStatus } from '@/api/pet';
import { columns } from './table';
import { Ellipsis } from '@/components';
export default {
    components: {
        Ellipsis,
    },
    data () {
        return {
            brandId: this.global.brandId,
            userName: '', // 用户名称
            userId: '', // 用户ID
            phone: '', // 手机号
            venue: '', // 活动地点
            orderCode: '', // 订单号
            paymentOrderCode: '', // 支付订单号
            startTime: '', // 开始时间
            endTime: '', // 结束时间
            speciesId: undefined, // 种类ID
            specList: [], // 种类列表
            storeId: undefined, // 门店ID
            storeList: [], // 门店列表
            serviceProjectId: undefined, // 服务
            serviceProjectList: [], // 服务列表
            paymentType: undefined,
            paymentTypeList: [
                { id: '0', name: '线上扣款'},
                { id: '1', name: '线下支付'},
            ],
            orderStatus: undefined, // 状态
            orderStatusList: [
                { id: '0', name: '初始化'},
                { id: '1', name: '已预约'},
                { id: '2', name: '已取消'},
                { id: '3', name: '已完成'},
            ],
            columns,
            dataList: [],
            total: 0,
            isload: false,
            page: 1,
            size: 10,
            colors: ['blue','purple','cyan','green','orange','red','pink','brown','indigo','lime','purple','teal','yellow','amber'],
        };
    },
    async created(){
        try {
            await this.querySpec();
            await this.queryStore();
            await this.queryServiceProject();
            this.getData()
        } catch (error) {
            this.$notification['error']({
                message: '提示',
                description: error,
                duration: 8,
            })
        }
    },
    methods: {
        // 查询宠物种类
        querySpec(){
            let _this = this
            return new Promise((resolve,reject) => {
                queryPetSpecies({'size': 99,'page': 0})
                .then(res=>{
                    if(res.success){
                        _this.specList = res.data.content;
                        resolve()
                    }else{
                        reject(res.errMessage)
                    }
                })
            })
        },
        // 查询宠物门店
        queryStore(){
            let _this = this
            return new Promise((resolve,reject) => {
                queryPetStore({'size': 99,'page': 0})
                .then(res=>{
                    if(res.success){
                        _this.storeList = res.data.content;
                        resolve()
                    }else{
                        reject(res.errMessage)
                    }
                })
            })
        },
        // 查询服务列表
        queryServiceProject(){
            let _this = this
            return new Promise((resolve,reject) => {
                queryPetServiceProject({'size': 99,'page': 0})
                .then(res=>{
                    if(res.success){
                        _this.serviceProjectList = res.data.content;
                        resolve()
                    }else{
                        reject(res.errMessage)
                    }
                })
            })
        },
        // 查询数据
        getData(){
            let _this = this
            _this.isload = true
            const params = {
                'brandId': _this.global.brandId,
                'userName': _this.userName,
                'userId': _this.userId,
                'phone': _this.phone,
                'speciesId': _this.speciesId,
                'storeId': _this.storeId,
                'serviceProjectId': _this.serviceProjectId,
                'venue': _this.venue,
                'orderCode': _this.orderCode,
                'paymentOrderCode': _this.paymentOrderCode,
                'startTime': _this.startTime,
                'endTime': _this.endTime,
                'orderStatus': _this.orderStatus,
                'size':_this.size,
                'page':_this.page - 1,
                'isNeedUserId': '0'
            }
            queryPetServiceOrderForm(params)
            .then(res=>{
                _this.isload = false
                if(res.success){
                    _this.dataList = res.data.content
                    _this.total = res.data.totalElements
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        // 分页
        handleTableChange(pagination, filters, sorter) {
            console.log(pagination);
            this.page = pagination.current;
            this.size = pagination.pageSize;
            this.getData()
        },
        updateOrderStatus(item,orderStatus){
            let _this = this;
            const params = {
                'orderCode': item.orderCode,
                'orderStatus': orderStatus,
            };
            updatePetServiceOrderStatus(params)
            .then(res => {
                if(res.success){
                    _this.getData();
                    _this.$notification['success']({
                        message: '提示',
                        description: '操作成功',
                        duration: 8,
                    })
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
    }
}

</script>
<style scoped>

</style>